package eproject.mm.com.projectsupervision.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.util.List;
import java.util.Map;

import eproject.mm.com.projectsupervision.R;
import eproject.mm.com.projectsupervision.adapter.ProjectSelectAdapter;
import eproject.mm.com.projectsupervision.tree.MyNodeViewFactory;
import eproject.mm.com.projectsupervision.util.HttpUtil;
import eproject.mm.com.projectsupervision.widget.IconFontTextview;
import me.texy.treeview.TreeNode;
import me.texy.treeview.TreeView;
import me.texy.treeview.TreeViewAdapter;

public class PlanNodeActivity extends Activity {
    LinearLayout ll_treeContainer;

    LoadingDialog loadingDialog = null;

    String prjId = "";
    IconFontTextview iftv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plan_node);

        initView();
        createTree();

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        prjId = bundle.getString("prjID");

        iftv_back = (IconFontTextview)findViewById(R.id.iftv_back);
        iftv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    private void initView(){
        ll_treeContainer = (LinearLayout)findViewById(R.id.ll_treeContainer);
    }
    private void createTree(){
        loadingDialog = new LoadingDialog(this);
        loadingDialog.setLoadingText("加载中...").show();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String scheduleStr = HttpUtil.get("/getScheduleList?projectid="+prjId);

                    Gson gson=new Gson();
                    Map resultMap = gson.fromJson(scheduleStr, Map.class);
                    if ((boolean) resultMap.get("success")) {
                        List rows = (List) resultMap.get("rows");

                        TreeNode root = TreeNode.root();
                        addSubTreeNode(root, rows, 0);

                        handler.sendMessage(handler.obtainMessage(1, root));
                    }else{
                        handler.sendMessage(handler.obtainMessage(2, null));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    handler.sendMessage(handler.obtainMessage(2, null));
                }
            }
        }).start();

//        TreeNode root = TreeNode.root();
//        for (int i = 0; i < 5; i++) {
//            TreeNode treeNode = new TreeNode(new String("Child " + "No." + i));
//            treeNode.setLevel(0);
//
//            for(int j=0;j<3;j++){
//                TreeNode treeSubNode = new TreeNode(new String("sub child " + "No." + i +"--"+j));
//                treeSubNode.setLevel(1);
//
//                if(j<2) {
//                    for (int k = 0; k < 3; k++) {
//                        TreeNode treeSubNode3 = new TreeNode(new String("sub child " + "No." + i + "--" + j + "--" + k));
//                        treeSubNode3.setLevel(2);
//                        treeSubNode.addChild(treeSubNode3);
//                    }
//                }
//                treeNode.addChild(treeSubNode);
//            }
//            root.addChild(treeNode);
//        }

    }

    public void back(View view) {
        finish();
    }

    private void addSubTreeNode(TreeNode parentNode, List subList,int level){
        for(int i=0;i<subList.size();i++){
            Map itemMap = (Map)subList.get(i);

            String id = (String)itemMap.get("id");
            String name = (String)itemMap.get("name");
            List subNodes = (List)itemMap.get("childrens");

            TreeNode subNode = new TreeNode(itemMap);
            //subNode.setValue(id);
            subNode.setLevel(level);

            if(subNodes!=null && subNodes.size()>0){
                subNode.setExpanded(true);
                addSubTreeNode(subNode, subNodes, level+1);
            }else{
                //subNode.setItemClickEnable(true);
            }

            parentNode.addChild(subNode);
        }
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    TreeNode root = (TreeNode)msg.obj;
                    View treeView = new TreeView(root, PlanNodeActivity.this, new MyNodeViewFactory(handler)).getView();
                    treeView.setLayoutParams(new ViewGroup.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                    ll_treeContainer.addView(treeView);
                    loadingDialog.close();

                    break;
                case 2:
                    loadingDialog.close();
                    break;
                case 5:
                    Map nodeMap = (Map)msg.obj;
                    String id = (String)nodeMap.get("id");
                    String name = (String)nodeMap.get("name");

                    Intent intent=new Intent();

                    Bundle bundle = new Bundle();
                    bundle.putString("id",id);
                    bundle.putString("name",name);

                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);

                    finish();
                    break;
                default:
                    super.handleMessage(msg);
                    break;
            }
        }
    };
}
